<template>
  <div>
    <van-row style="padding-top:1rem;padding-bottom: 1rem">
      <van-col span="12">
        <table>
          <tr>
            <th>工作流编号</th>
            <th>工作流名称</th>
<!--            <th>工作流参数</th>-->
          </tr>
          <tr v-for="(i,index) in worklists" @click="selectwork(index)" :key="i.id">
            <td>{{i.id}}</td>
            <td>{{i.name}}</td>
          </tr>
        </table>
      </van-col>
      <van-col span="12"><p>{{worklist['name']}}</p>
        <div v-for="i in worklist['params']" :key="i.id" align="left">
          <van-field :label="i.name" v-if="i.type=='input'" v-model="tfvalue[i.name]" />
          <el-select  v-if="i.type=='select'" v-model="tfvalue[i.name]">
            <el-option :value="j.name" v-for="j in i['value']" :key="j.name">{{j.name}}</el-option>
          </el-select>
          <div class="block" v-if="i.type=='datetime'" >
            <el-date-picker
                v-model="tfvalue[i.name]"
                type="datetimerange"
                start-placeholder="Start Date"
                end-placeholder="End Date"
                :default-time="defaultTime"
            >
            </el-date-picker>
          </div>
          <van-uploader v-if="i.type=='file'" v-model="tfvalue[i.name]" />
        </div>
        <div>
          <el-dialog v-model="show">
            <el-tree
                ref="tree"
                :data="data1"
                show-checkbox
                default-expand-all
                node-key="id"
                highlight-current
                :props="defaultProps"
                @check="selectid"
            />
            <van-button @click="sureapprover">确认审批人</van-button>
          </el-dialog>
          <font color="#483d8b" @click="selectapprover" v-if="id!='x'">选择审批人</font>
        </div>
        <van-button @click="add">提交</van-button>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import myaxios from "../../../https/myaxios";

export default {
  name: "workf_show",
  data(){
    return{
      id:'x',
      worklists:[],
      worklist:{},
      // 时间
      value1:[],
      // 默认时间
      defaultTime:[new Date(2021,12,22,0,0,0)],
      // 绑定的数据
      tfvalue:{},
      show:false,
      data1:[],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      // 选择审批人
      slist:[],
      // 审批人过滤空值
      slist1:[],


    }
  },
  methods:{
    sureapprover(){
      let lst=[]
      for(var i=0;i<this.slist.length;i++){
        if(this.slist[i]){
          lst.push(this.slist[i])
        }
      }
      this.slist1=lst
      this.show=false
    },
    selectid(val){
        this.slist.push(val["rid"])
      console.log(this.slist)
    },
    selectapprover(){
      this.show=true
      myaxios("selectapprover","get").then(res=>{
        this.data1=res.list
        console.log(this.data1)
      })
    },
    changetime(mytime){
      const mydate = String(mytime).split(" ")
        // console.log(mydate[3],mydate[1],mydate[2])
      if(mydate[1]=="Jan"){
        mydate[1]="01"
      }else if(mydate[1]=="Feb"){
        mydate[1]="02"
      } else if(mydate[1]=="Mar"){
        mydate[1]="03"
      }else if(mydate[1]=="Apr"){
        mydate[1]="04"
      }else if(mydate[1]=="Mar"){
        mydate[1]="05"
      }else if(mydate[1]=="Jun"){
        mydate[1]="06"
      }else if(mydate[1]=="Jul"){
        mydate[1]="07"
      }else if(mydate[1]=="Aug"){
        mydate[1]="08"
      }else if(mydate[1]=="Sep"){
        mydate[1]="09"
      }else if(mydate[1]=="Oct"){
        mydate[1]="10"
      }else if(mydate[1]=="Nov"){
        mydate[1]="11"
      }else if(mydate[1]=="Dec"){
        mydate[1]="12"
      }
      let time = mydate[3] + "-" + mydate[1] + "-" + mydate[2]+' '+ mydate[4]
      return time
    },
    add(){
      console.log(this.tfvalue)
      this.tfvalue["时间"][0]=this.changetime(this.tfvalue["时间"][0])
      this.tfvalue["时间"][1]=this.changetime(this.tfvalue["时间"][1])
      let data = new FormData()
      data.append("userid",localStorage.getItem("userid"))
      data.append("title",this.worklist["name"])
      data.append("params",JSON.stringify(this.tfvalue))
      data.append("approver",JSON.stringify(this.slist1))
      myaxios("addtask","post",data).then(res=>{
        alert(res.msg)
      })

    },
    selectwork(index){
      this.id=index
      this.worklist=this.worklists[index]
      console.log(this.worklist)
    },
    getworkf(){
      myaxios("query_workflow","get").then(res=>{
        let lst = res.list
        for(var i =0;i<lst.length;i++){
          lst[i]["params"]=JSON.parse(res.list[i]["params"])
        }
        this.worklists=lst
        // console.log(this.worklists)
      })
    }
  },
  mounted() {
    this.getworkf()
  }
}
</script>

<style scoped>

</style>